<?php
namespace app\api\model;

use think\Model;

class UserShop extends Base{
    protected $name = 'user_shop';

    /** 根据总消费更新用户在指定店铺的星级 */
    public function setStar($user_id, $admin_id){
        $userShopInfo = $this->getFind(['user_id' => $user_id, 'admin_id' => $admin_id])['data'];

        $starModel = new Star();

        $star_info = $starModel->where([['price', '<=', $userShopInfo['money_total']]])->order('id DESC')->find();

        if($star_info && $userShopInfo['star'] < $star_info['id']){
            if($star_info['term_time'] > 0){
                $time = time() + $star_info->getData('term_time') * 30 * 86400;
            }else{
                $time = 0;
            }

            $this->editWhere(['star_id' => $star_info['id'], 'star_time' => $time], ['user_id' => $user_id, 'admin_id' => $admin_id]);
        }
    }

    public function getStarTimeAttr($val){
        if(!$val){
            return '永久';
        }

        return date('Y-m-d H:i:s', $val);
    }

    public function getGoldTimeAttr($val){
        if(!$val){
            return '永久';
        }

        return date('Y-m-d H:i:s', $val);
    }
}